<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class FgcmsContact {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		//contructor function
		$this->db = $db;
		$this->smarty = $smarty;
		$this->ConfigBean = new FgcmsConfigBean();
	}
	
	public function view_formcontact() {
		if($_POST['act']=='sendcontact')
		{
			$ret = "";		
			$ret = $this->validate();
			if($ret['dem'] == 0) {
				$this->send_contact_email($_POST);
				$type = array('ID' => 0, 'message' => $ret);
				echo array2json($type);	
			}else{
				$type = array('ID' => -1, 'message' => $ret);
				echo array2json($type);		
			}	
			//else
			//$message = "Code falsch eingegeben";	
			//$this->smarty->assign('FORM_CONTACT',$_POST);	
			//$this->smarty->assign('MESSAGE',$message);	
		}else if(isset($_POST['act']) && $_POST['act']=='checkerror')
			$this->checkerror();	
		if(isset($_GET['act']) && $_GET['act']=='success')
		{
			$page->page_name = "Contact Successful";
			$page->page_content = $this->smarty->fetch('contact_success.tpl');
			$this->smarty->assign('PAGE',$page);			
		}
		else
		{
			$UDS = FgcmsConfigBean::getconfig($this->db,"GOOGLE_UDS_KEY");
			$API = FgcmsConfigBean::getconfig($this->db,"GOOGLE_API_KEY");
			$address = FgcmsConfigBean::getconfig($this->db,"GOOGLE_ADDRESS");
			$this->smarty->assign("GOOGLE_UDS",$UDS->value);
			$this->smarty->assign("GOOGLE_API",$API->value);
			$this->smarty->assign("ADDRESS",$address->value);
			$page->page_name = "Kontaktformular";
			$page->page_content = $this->smarty->fetch('contact.tpl');
			$this->smarty->assign('PAGE',$page);
		}
	$this->smarty->assign('MENUPARENT','Kontakt');	
	}
	
	public function send_contact_email($post){
		$contact_email = $this->ConfigBean->getconfig($this->db,"CONTACT_EMAIL");
		
		$to = $contact_email->value;
		$from = $post['email'];
		$reply = $contact_email->value;
		$subject = "Contact";
		$this->smarty->assign('CONTENT',$post);
		$str = $this->smarty->fetch("email_contact.tpl");
		$message = $str;
		$header = "Content-type: text/html\r\nFrom: $from\r\nReply-to: $reply";
 
		mail($to, $subject, $message, $header); 

	}
	
	public function checkerror() {
		$ret = $this->validate();
		$type = array('ID' => -1, 'message' => $ret);
		echo array2json($type);	
	}
	
	public function check_email_address($email) {
    	// First, we check that there's one @ symbol, and that the lengths are right
    	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    	// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    	return false;
    	}
    	// Split it into sections to make life easier
    	$email_array = explode("@", $email);
    	$local_array = explode(".", $email_array[0]);
   		for ($i = 0; $i < sizeof($local_array); $i++) {
   			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
   			return false;
   			}
   		}
   		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))  // Check if domain is IP. If not, it should be valid domain name
   		{
   			$domain_array = explode(".", $email_array[1]);
   			if (sizeof($domain_array) < 2) 
   			{
   			return false; // Not enough parts to domain
   			}
   			for ($i = 0; $i < sizeof($domain_array); $i++) 
   			{
   				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) 
   				{
   				return false;
   				}
   			}
   		}
   		return true;
	}
	
	public function validate()
    {
    	$msg = array();
		$msg["name"] = '';
		$msg["email"] = '';
		$msg["telephone"] = '';
		$msg["captcha"] = '';
;
		$dem =0;
	
		if(isset($_POST['name']) && $_POST['name'] == ""){
			$msg["name"] = "Your first name could not be empty.";
			$dem++;
		}
		if(isset($_POST['captcha']) && $_POST['captcha'] == ""){
			$msg["captcha"] = "Your code could not be empty.";
			$dem++;
		}else{
			if($_POST['captcha'] != $_SESSION['captcha_str']){
				$msg["captcha"] = "Code falsch eingegeben.";	
				$dem++;	
			}
		}
		if(isset($_POST['telephone']) && $_POST['telephone'] == ""){
			$msg["telephone"] = "Your Telephone could not be empty.";
			$dem++;
		}
		if(isset($_POST['telephone']) && $_POST['telephone'] != "" && (strlen($_POST['telephone']) < 6 || (!preg_match('/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/', $_POST['telephone'])))){
			$msg["telephone"]= "Invalid telephone number.";
			$dem++;
		}
		
		if(isset($_POST['email']) && $_POST['email'] == ""){
			$msg["email"]= "Your email address could not be empty.";
			$dem++;
		}
		if(isset($_POST['email']) && $_POST['email'] != ""){
			$flag = $this->check_email_address($_POST['email']);
			if($flag == false){
				$msg["email"]= "Invalid email address.";
				$dem++;
			}
		}
		
		$msg["dem"] = $dem;
		return $msg;
    }
}
?>
